package com.javabasic.crawler;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/5/24 10:05
 * @Version 1.0
 * @TODO 将中文句子换一种说法 通过：中—>英->日->中—>英->日->中  的转变方式
 **/
public class ConvertSentence {

    private static Logger logger = LoggerFactory.getLogger(ConvertSentence.class);

    private static Scanner cin = new Scanner(System.in);

    private static String URL = "https://translate.google.cn/translate_a/single?client=webapp&sl=auto&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=sos&dt=ss&dt=t&ssel=3&tsel=3&kc=0&tk=158140.316543";
    private static OkHttpClient client = new OkHttpClient();
    private static Headers headers = null;
    // 文本转为中文的URL
    private static HttpUrl.Builder urlBuilderTextToCn = null;
    // 文本转为英文的URL
    private static HttpUrl.Builder urlBuilderTextToEn = null;
    // 文本转为日文的URL
    private static HttpUrl.Builder urlBuilderTextToJa = null;
    //URL带的参数
    private static HashMap<String,String> params = new HashMap<>();


    // TODO 执行初始化操作
    public static void init() {
        headers = new Headers.Builder()
                //.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
                .add("Connection","keep-alive")
                .build();

        // 目标语言
        params.put("tl","zh-CN");
        // 源语言
        params.put("hl","ja");

        urlBuilderTextToCn = urlAddParam(urlBuilderTextToCn,params);

        params.put("tl","en");
        // 源语言
        params.put("hl","zh-CN");
        urlBuilderTextToEn = urlAddParam(urlBuilderTextToEn,params);


        params.put("tl","ja");
        // 源语言
        params.put("hl","en");
        urlBuilderTextToJa = urlAddParam(urlBuilderTextToJa,params);
    }

    private static HttpUrl.Builder urlAddParam(HttpUrl.Builder urlBuilder, HashMap<String, String> params) {
        urlBuilder = HttpUrl.parse(URL).newBuilder();
        if (ConvertSentence.params != null) {
            for (String key : ConvertSentence.params.keySet()) {
                urlBuilder.setQueryParameter(key, ConvertSentence.params.get(key));
            }
        }
        return urlBuilder;
    }

    public static void main(String[] args) throws IOException {

        System.out.println("=====================转换开始=====================");


        String text = "中国你好棒";
        // TODO 中转英
        params.put("q",text);
        init();
        Request request = new Request.Builder()
                .url(urlBuilderTextToEn.build())
                .headers(headers)
                .get()
                .build();
        System.out.println(request.toString());
        Response response = client.newCall(request).execute();
        text = response.body().string();
        System.out.println(text);
//        while (convertExecute());

        System.out.println("=====================转换结束=====================");
    }

    private static boolean convertExecute() {
        System.out.println("请输入您要转换的中文文本：");
        String text = cin.next();
        if (StringUtils.isBlank(text)) {
            System.out.println("亲，您未输入文本，不需要转换!!!");
            return true;
        }
        if ("exit".equals(text)) {
            return false;
        }
        System.out.print("转换后的结果是：");


        Response response = null;
        Request request = null;
        try {
            for (int i = 0; i < 3; i++) {
                // TODO 中转英
                params.put("text",text);
                urlAddParam(urlBuilderTextToEn,params);
                request = new Request.Builder()
                        .url(urlBuilderTextToEn.build())
                        .headers(headers)
                        .get()
                        .build();
                response = client.newCall(request).execute();
                text = response.body().string();

                // TODO 英转日
                params.put("text",text);
                urlAddParam(urlBuilderTextToJa,params);
                request = new Request.Builder()
                        .url(urlBuilderTextToJa.build())
                        .headers(headers)
                        .get()
                        .build();
                response = client.newCall(request).execute();
                text = response.body().string();

                // TODO 日转中
                params.put("text",text);
                urlAddParam(urlBuilderTextToCn,params);
                request = new Request.Builder()
                        .url(urlBuilderTextToCn.build())
                        .headers(headers)
                        .get()
                        .build();
                response = client.newCall(request).execute();
                text = response.body().string();
            }
            System.out.println(text);
        } catch (IOException e) {
            logger.error(e.getMessage());
        } finally {
            return true;
        }
    }
}
